import { ContainerService } from "shared/component/container/ContainerService";
import Mouter from "shared/component/dlt-view/Mouter";
import EventBinder from "shared/component/event/EventBinder";
import Log4R from "shared/component/log/Log4R";
import { ScrennDisplayOrder } from "shared/enums/ScreenDisplayOrder";
import { PcActPanel } from "../view/pc/PcActPanel";
import Roact from "@rbxts/roact";
import newIns from "shared/component/instace/RobloxInstance";
import common from "shared/component/common/common";

const loginfo = Log4R.getLog('info');


export class ControllerServiceImpl extends ContainerService<Ctn.ControllerService, Ctn.ControllerEntity> implements Ctn.ControllerService {
    protected serverCalls: (keyof this)[] = [];
    protected serviceName: keyof Ctn.Services = "controllerService";

    /**
     * 控制器遥感方向改变事件
     */
    private stickChangeEventBinder = new EventBinder<(vector: Vector3) => void>()
    /**
     * 控制器遥感方向改变事件
     */
    private actButtonEventBinder = new EventBinder<(actButtonState: Ctn.controller.ActButtonStatesEntity) => void>()
    /**
     * 摇杆方向改变事件
     */
    onStickChange = this.stickChangeEventBinder.bind()
    onActButtonChange = this.actButtonEventBinder.bind();
    vectorCache = new Instance('Vector3Value');
    public setStickVector(vector: Vector3) {
        const oldValue = this.vectorCache.Value;
        if (oldValue !== vector) {
            this.vectorCache.Value = vector
            this.stickChangeEventBinder.callBack(vector)
        }
    }
    showActPanel(buttons: Array<{
        inputType: Enum.UserInputType,
        key: Enum.KeyCode,
        act: Ctn.Act
    }> = [
            {
                inputType: Enum.UserInputType.Keyboard,
                key: Enum.KeyCode.U,
                act: 'SkillA'
            },
            {
                inputType: Enum.UserInputType.Keyboard,
                key: Enum.KeyCode.I,
                act: 'jump'
            },
            {
                inputType: Enum.UserInputType.Keyboard,
                key: Enum.KeyCode.O,
                act: 'dodge'
            }
        ]) {

        const screen = newIns("ScreenGui", "ActPanel", this.getPlayerGui());
        // loginfo?.info('showActPanel', screen)
        screen.DisplayOrder = ScrennDisplayOrder.controller;
        screen.ScreenInsets = Enum.ScreenInsets.None;
        const mouter = new Mouter();
        if (common.getTouchEnabled()) {

        } else {
            mouter.mount(<PcActPanel mounter={mouter}
                actButtonEventBinder={this.actButtonEventBinder}
                buttons={buttons}></PcActPanel>, screen)
        }


    }
}